import { _decorator, Component, Node, BoxCollider, Vec3, macro, randomRange, RigidBody } from 'cc';
import { Actor } from '../actor/Actor';
import { ActorManager } from './ActorManager';
import { EffectManager } from './EffectManager';
const { ccclass, property } = _decorator;

@ccclass('Level')
export class Level extends Component {
    @property(BoxCollider)
    spawnCollider: BoxCollider = null
    spawnPos: Vec3 = new Vec3()

    baseHp: number = 1

    count: number = 10
    maxAlive: number = 50

    start() {
        ActorManager.instance.init(() => {
            this.startLevelTimer()
            // EffectManager.instance.init(() => {
            //     this.startLevelTimer()
            // })
        })
    }
    onDestroy() {
        // LevelManager.instance = null
        ActorManager.instance.destory()
        // EffectManager.instance.destory()
    }
    startLevelTimer() {
        //生成怪物
        this.schedule(() => {
            if (ActorManager.instance.enemies.length > this.maxAlive) return
            for (let i = 0; i < this.count; i++) this.randomSpawn()
        }, 10, macro.REPEAT_FOREVER, 1)

        //怪物增强
        this.schedule(() => {
            this.baseHp *= 1.2
        }, 20, macro.REPEAT_FOREVER, 1)
    }
    update(deltaTime: number) {

    }
    randomSpawn() {
        this.spawnPos.x = randomRange(-this.spawnCollider.size.x, this.spawnCollider.size.x)
        this.spawnPos.z = randomRange(-this.spawnCollider.size.z, this.spawnCollider.size.z)

        let node = ActorManager.instance.createEnemy()
        node.worldPosition = this.spawnPos

        let scale = randomRange(1.0, 2.0)
        let actor = this.getComponent(Actor)

        // 随机大小的血量
        // actor.actorProp.hp = this.baseHp * scale
        //随机大小的体型
        node.setWorldScale(scale, scale, scale)
        //随机大小的质量
        let rigidbody = node.getComponent(RigidBody)
        rigidbody.mass = scale

    }
}


